#!/usr/bin/python
#
#       openmiaocat.py
#       
#       Copyright 2009 Pietro Isotti <isottipietro@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import time, os, string, pygame
from time import sleep

def draw_cat(screen):
		global img_cat
		#screen.fill((0,0,0))
		screen.blit(img_cat,(0, -32))
		pygame.display.update()

def purr(cmd):
	global pet, started
	if cmd:
		os.system("echo \"25\" > /sys/devices/platform/neo1973-vibrator.0/leds/neo1973:vibrator/brightness")
		started = True
	if cmd and started:
		os.system("echo \"50\" > /sys/devices/platform/neo1973-vibrator.0/leds/neo1973:vibrator/brightness")
	elif not cmd:
		started = False
		pet = 0
		os.system("echo \"40\" > /sys/devices/platform/neo1973-vibrator.0/leds/neo1973:vibrator/brightness")
		sleep(0.5)
		os.system("echo \"30\" > /sys/devices/platform/neo1973-vibrator.0/leds/neo1973:vibrator/brightness")
		sleep(0.5)
		os.system("echo \"20\" > /sys/devices/platform/neo1973-vibrator.0/leds/neo1973:vibrator/brightness")
		sleep(0.5)
		os.system("echo \"0\" > /sys/devices/platform/neo1973-vibrator.0/leds/neo1973:vibrator/brightness")
		
pygame.init()
SCREEN_SIZE = (480,640)
pet = 0
state = "up"
started = False
img_cat = pygame.image.load('/usr/share/pixmaps/openmiaocat/cat.png')
screen = pygame.display.set_mode(SCREEN_SIZE,0,32)
pygame.display.set_caption("MiaoCat")
#pygame.display.toggle_fullscreen()
pygame.mouse.set_visible(False)
draw_cat(screen)
t = time.time()
while 1:
	tmp=pygame.event.get()
	for i in tmp:
			print str(i)
			if string.find(str(i), "MouseButtonDown")!=-1:
				state="down"
			if string.find(str(i), "MouseButtonUp")!=-1:
				state="up"
				pet+=1
				t = time.time()
	if pet==2: purr(True)
	if pet==3: purr(True)
	if time.time()-t>3 and started: purr(False)
	if string.find(str(tmp), "12-Quit {}")!=-1:
		os.system("echo \"0\" > /sys/devices/platform/neo1973-vibrator.0/leds/neo1973:vibrator/brightness")
		pygame.quit()
		exit()
